/*
 * Copyright 2005 MH-Software-Entwicklung. All rights reserved.
 * Use is subject to license terms.
 */
package com.jtattoo.plaf.acryl;

import java.awt.*;
import javax.swing.*;

import com.jtattoo.plaf.*;

/**
 * @author Michael Hagen
 */
public class AcrylInternalFrameTitlePane extends BaseInternalFrameTitlePane {

	public AcrylInternalFrameTitlePane(JInternalFrame f) {
		super(f);
	}

	protected LayoutManager createLayout() {
		return new BaseTitlePaneLayout();
	}

	protected int getHorSpacing() {
		return 1;
	}

	protected int getVerSpacing() {
		return 3;
	}

	public void paintBorder(Graphics g) {
		if (isActive()) {
			g.setColor(AcrylLookAndFeel.getWindowBorderColor());
		} else {
			g.setColor(AcrylLookAndFeel.getWindowInactiveBorderColor());
		}
		g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
	}

	public void paintText(Graphics g, int x, int y, String title) {
		if (isActive()) {
			Color shadowColor = ColorHelper.darker(AcrylLookAndFeel.getWindowTitleColorDark(), 30);
			g.setColor(shadowColor);
			JTattooUtilities.drawString(frame, g, title, x - 1, y - 1);
			JTattooUtilities.drawString(frame, g, title, x - 1, y + 1);
			JTattooUtilities.drawString(frame, g, title, x + 1, y - 1);
			JTattooUtilities.drawString(frame, g, title, x + 1, y + 1);
			g.setColor(AbstractLookAndFeel.getWindowTitleForegroundColor());
		} else {
			g.setColor(AbstractLookAndFeel.getWindowInactiveTitleForegroundColor());
		}
		JTattooUtilities.drawString(frame, g, title, x, y);
	}

	//--------------------------------------------------------------------------------------------
	class BaseTitlePaneLayout extends TitlePaneLayout {

		public void addLayoutComponent(String name, Component c) {
		}

		public void removeLayoutComponent(Component c) {
		}

		public Dimension preferredLayoutSize(Container c) {
			return minimumLayoutSize(c);
		}

		public Dimension minimumLayoutSize(Container c) {
			int width = 30;
			if (frame.isClosable()) {
				width += 21;
			}
			if (frame.isMaximizable()) {
				width += 16 + (frame.isClosable() ? 10 : 4);
			}
			if (frame.isIconifiable()) {
				width += 16 + (frame.isMaximizable() ? 2 : (frame.isClosable() ? 10 : 4));
			}
			FontMetrics fm = getFontMetrics(getFont());
			String frameTitle = frame.getTitle();
			int title_w = frameTitle != null ? fm.stringWidth(frameTitle) : 0;
			int title_length = frameTitle != null ? frameTitle.length() : 0;

			if (title_length > 2) {
				int subtitle_w = fm.stringWidth(frame.getTitle().substring(0, 2) + "...");
				width += (title_w < subtitle_w) ? title_w : subtitle_w;
			} else {
				width += title_w;
			}

			int height = paletteTitleHeight;
			if (!isPalette) {
				int fontHeight = fm.getHeight() + 7;
				Icon icon = frame.getFrameIcon();
				int iconHeight = 0;
				if (icon != null) {
					iconHeight = Math.min(icon.getIconHeight(), 18);
				}
				iconHeight += 5;
				height = Math.max(fontHeight, iconHeight);
			}
			return new Dimension(width, height);
		}

		public void layoutContainer(Container c) {
			boolean leftToRight = JTattooUtilities.isLeftToRight(frame);

			int spacing = getHorSpacing();
			int w = getWidth();
			int h = getHeight();

			// assumes all buttons have the same dimensions these dimensions include the borders
			int buttonHeight = h - getVerSpacing();
			int buttonWidth = buttonHeight + 10;

			int x = leftToRight ? w - spacing : 0;
			int y = Math.max(0, ((h - buttonHeight) / 2) - 1);

			if (frame.isClosable()) {
				x += leftToRight ? -buttonWidth : spacing;
				closeButton.setBounds(x, y, buttonWidth, buttonHeight);
				if (!leftToRight) {
					x += buttonWidth;
				}
			}

			if (frame.isMaximizable() && !isPalette) {
				x += leftToRight ? -spacing - buttonWidth : spacing;
				maxButton.setBounds(x, y, buttonWidth, buttonHeight);
				if (!leftToRight) {
					x += buttonWidth;
				}
			}

			if (frame.isIconifiable() && !isPalette) {
				x += leftToRight ? -spacing - buttonWidth : spacing;
				iconButton.setBounds(x, y, buttonWidth, buttonHeight);
				if (!leftToRight) {
					x += buttonWidth;
				}
			}

			buttonsWidth = leftToRight ? w - x : x;
		}
	} // end class BaseTitlePaneLayout
}
